export const disabledDate = (time: Date) => {
  return time.getTime() > Date.now()
}

export function parseTime(time: any, cFormat?: string) {
  if (!time) {
    return
  }
  if (arguments.length === 0) {
    return null
  }

  if ((time + '').length === 10) {
    time = +time * 1000
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time == 'object') {
    date = time
  } else {
    if (typeof time == 'string') {
      if (time.includes('T')) {
        date = new Date(time).toISOString().substring(0, 19).replace('T', ' ')
        date = new Date(time)
      } else {
        date = new Date(parseInt(time))
      }

    } else if (typeof time == 'number') {
      date = new Date(time)
    }
  }
  const formatObj: { [key: string]: any } = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: any, key: string) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


// 开始递归方法
export function generateOptions(params: any[]) {
  var result = []
  for (const param of params) {
    if (!param.parent_id) {  // 判断是否为顶层节点
      let { id, name, longitude, latitude, level, code, parent_id } = param;
      var parent: any = {
        'id': id,
        'value': id,
        'label': name,
        parent_id
      }
      if (param.hasOwnProperty('longitude')) {
        parent = {
          ...parent,
          longitude,
          latitude,
          level,
          code
        }
      }

      parent.children = getchilds(param.id, params)  // 获取子节点
      result.push(parent)
    }
  }
  return result

}

function getchilds(id: string | number, array: any[]) {
  const childs = []
  for (const arr of array) {  // 循环获取子节点
    if (arr.parent_id === id) {
      let { id, name, longitude, latitude, level, code, parent_id } = arr;
      if (arr.hasOwnProperty('longitude')) {
        childs.push({
          id,
          'value': id,
          'label': name,
          longitude,
          latitude,
          level,
          code,
          parent_id
        })
      } else {
        childs.push({
          id,
          'value': id,
          'label': name,
          parent_id
        })
      }
    }
  }
  for (const child of childs) { // 获取子节点的子节点
    const childscopy = getchilds(child.id, array)// 递归获取子节点
    if (childscopy.length > 0) {
      child.children = childscopy
    }
  }
  return childs
}

// 开始递归方法
export function generateOptions1(params: any[]) {
  var result = []
  for (const param of params) {
    if (!param.parent) {  // 判断是否为顶层节点
      var parent: any = {
        'id': param.id,
        'value': param.id,
        'label': param.name
      }
      parent.children = getchilds1(param.id, params)  // 获取子节点
      result.push(parent)
    }
  }
  return result

}

function getchilds1(id: string | number, array: any[]) {
  const childs = []
  for (const arr of array) {  // 循环获取子节点
    if (arr.parent === id) {
      childs.push({
        'id': arr.id,
        'value': arr.id,
        'label': arr.name
      })
    }
  }
  for (const child of childs) { // 获取子节点的子节点
    const childscopy = getchilds(child.id, array)// 递归获取子节点
    if (childscopy.length > 0) {
      child.children = childscopy
    }
  }
  return childs
}

// js实现根据树子节点id查找所有父节点id

export function findParentIds(array: any[], id: string) {
  let parentArray = [];
  if (array.length === 0) {
    return parentArray;
  }

  function recursion(arrayNew: any[], id: string) {
    
    for (let i = 0; i < arrayNew.length; i++) {
      let node = arrayNew[i];
      // debugger
      if (node.id === id) {
        parentArray.unshift(id);
        recursion(array, node.parent_id);
        break;
      } else {
        if (node.children && node.children.length) {
          recursion(node.children, id);
        }
      }
    }
    return parentArray;
  }
  let arrayNew = array;
  parentArray = recursion(arrayNew, id);
  console.log(parentArray)
  return parentArray;
}